if SERVER then
	AddCSLuaFile("shared.lua")
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
	SWEP.HoldType = "pistol"
	SWEP.PlayOnce = false
end

if CLIENT then

	surface.CreateFont("csd", ScreenScale(30), 500, true, true, "CSKillIcons")
	surface.CreateFont("csd", ScreenScale(60), 500, true, true, "CSSelectIcons")

	SWEP.DrawAmmo			= true
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 60
	SWEP.ViewModelFlip		= false
	SWEP.CSMuzzleFlashes	= true
	SWEP.PrintName = "Base SWEP"
	SWEP.IconLetter = "a"
	SWEP.IconFont = "CSSelectIcons"
	
	function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )
		draw.SimpleText( self.IconLetter, self.IconFont, x + wide/2, y + tall*0.2, Color( 15, 20, 200, 255 ), TEXT_ALIGN_CENTER )
	end
	
end

SWEP.Author			= "Rambo_6"
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= ""

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= false

SWEP.ViewModel	= "models/weapons/v_pistol.mdl"
SWEP.WorldModel = "models/weapons/w_pistol.mdl"

SWEP.Primary.Sound			= Sound("Weapon_USP.Single")
SWEP.Primary.Reload			= Sound("Weapon_USP.Single")
SWEP.Primary.Recoil			= 3.5
SWEP.Primary.Damage			= 1
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.025
SWEP.Primary.Delay			= 0.15

SWEP.Primary.ClipSize		= 50
SWEP.Primary.DefaultClip	= 99999
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "pistol"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

function SWEP:GetWorldModel()
	return self.WorldModel
end

function SWEP:GetUsedAmmoType()
	return self.Primary.Ammo
end

function SWEP:Initialize()
	if SERVER then
		self:SetWeaponHoldType(self.HoldType)
	end
end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
	return true
end  

function SWEP:Think()	
	
end

function SWEP:CanPrimaryAttack()
	if self.Weapon:Clip1() <= 0 then
		self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
		self.Weapon:EmitSound( self.Primary.Reload, 100, 100 )
		self.Weapon:DefaultReload( ACT_VM_RELOAD )
		return false
	end
	return true
end

function SWEP:SecondaryAttack()

end

function SWEP:PrimaryAttack()
	if not self:CanPrimaryAttack() then return end
	
	self.PlayOnce = false
	self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)

	self.Weapon:EmitSound(self.Primary.Sound,100,math.random(97,103))
	self:ShootBullets(self.Primary.Damage, self.Primary.NumShots, self.Primary.Cone)
	self:TakePrimaryAmmo(1)
	self:SetRecoilTime(CurTime() + .2)
	
	//if SERVER then
		//self.Owner:ViewPunch(Angle(math.Rand(-0.2,-0.1) * self.Primary.Recoil, math.Rand(-0.1,0.1) *self.Primary.Recoil, 0))
	//end
end

function SWEP:ShootBullets( damage, numbullets, aimcone )

	local scale = 1
	local canscale = true
	if self.Owner:KeyDown(IN_DUCK) then
		scale = math.Clamp(aimcone/1.5,0,10)
		canscale = false
	elseif self.Owner:KeyDown(IN_WALK) and canscale then
		if self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT) then
			scale = aimcone
			canscale = false
		else
			scale = aimcone
			canscale = false
		end
	elseif (self.Owner:KeyDown(IN_SPEED) or self.Owner:KeyDown(IN_JUMP)) and canscale then
		scale = aimcone + (aimcone*2)
		canscale = false
	elseif (self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT)) and canscale then
		scale = aimcone + (aimcone*1.5)
	else
		scale = aimcone
	end
	
	local recoil = self.Weapon:GetRecoilRatio()
	scale = scale * recoil
	
	local bullet = {}
	bullet.Num 		= numbullets
	bullet.Src 		= self.Owner:GetShootPos()			
	bullet.Dir 		= self.Owner:GetAimVector()			
	bullet.Spread 	= Vector( scale, scale, 0 )		
	bullet.Tracer	= 1	
	bullet.Force	= math.Round(damage * 1.1)							
	bullet.Damage	= math.Round(damage)
	bullet.AmmoType = "Pistol"
	bullet.TracerName 	= "Tracer"
	bullet.Callback = function ( attacker, tr, dmginfo )
		if tr.HitWorld or not tr.Entity then return end
		if tr.Entity:IsPlayer() then
			util.Decal("Blood", tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal)
		end
	end
	
	self.Owner:FireBullets( bullet )
	
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) 		// View model animation
	self.Owner:MuzzleFlash()							// Crappy muzzle light
	self.Owner:SetAnimation( PLAYER_ATTACK1 )			// 3rd Person Animation
	
end

function SWEP:SetRecoilTime( t )
	self.Weapon:SetNetworkedFloat( "Recoil", t )
end

function SWEP:GetRecoilTime()
return self.Weapon:GetNetworkedFloat( "Recoil" ) or CurTime() end

function SWEP:GetRecoilRatio()
	local rtime = self.Weapon:GetRecoilTime()
	local ratio = 2 - math.Clamp( (CurTime() - rtime) * 4, 0.0, 1.0 )
	return ratio
end

SWEP.CrosshairScale = 1
function SWEP:DrawHUD()

	local x = ScrW() * 0.5
	local y = ScrH() * 0.5
	local scalebywidth = (ScrW() / 1024) * 10

	local scale = 1
	local canscale = true
	if self.Owner:KeyDown(IN_DUCK) then
		scale = scalebywidth * math.Clamp(self.Primary.Cone/1.5,0,10)
		canscale = false
	elseif self.Owner:KeyDown(IN_WALK) and canscale then
		if self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT) then
			scale = scalebywidth * self.Primary.Cone
			canscale = false
		else
			scale = scalebywidth * self.Primary.Cone
			canscale = false
		end
	elseif (( self.Owner:KeyDown(IN_SPEED) and self.Owner:GetVelocity() != Vector(0,0,0) ) or self.Owner:KeyDown(IN_JUMP)) and canscale then
		scale = scalebywidth * self.Primary.Cone + (self.Primary.Cone*12)
		canscale = false
	elseif (self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT)) and canscale then
		scale = scalebywidth * self.Primary.Cone + (self.Primary.Cone*6)
	else
		scale = scalebywidth * self.Primary.Cone
	end
	
	local recoil = self.Weapon:GetRecoilRatio()
	scale = scale * recoil

	if self.Owner:KeyDown(IN_ZOOM) then
		scale = scale * 5
	end

	surface.SetDrawColor(220, 220, 220, 220)

	local dist = math.abs(self.CrosshairScale - scale)
	self.CrosshairScale = math.Approach(self.CrosshairScale, scale, FrameTime() * 2 + dist * 0.05)

	local gap = 40 * self.CrosshairScale
	local length = gap + 15 * self.CrosshairScale
	surface.DrawLine(x - length, y, x - gap, y)
	surface.DrawLine(x + length, y, x + gap, y)
	surface.DrawLine(x, y - length, x, y - gap)
	surface.DrawLine(x, y + length, x, y + gap)
	
end

function SWEP:Reload()
	self.Weapon:DefaultReload( ACT_VM_RELOAD )
	if not self.PlayOnce then
		self.PlayOnce = true
		self.Weapon:EmitSound( self.Primary.Reload, 100, 100 )
	end
end
